package com.module_video.popuputil;

import android.app.Activity;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.lib.fastkit.utils.share.tool.StringUtil;
import com.luck.picture.lib.tools.ToastManage;
import com.module_video.R;
import com.zyyoona7.popup.EasyPopup;

/**
 * Created by Administrator on 2020/5/14 0014.
 */

public class BottomCommentPopup implements View.OnClickListener {

    private Activity activity;

    public BottomCommentPopup(Activity activity) {

        this.activity = activity;

        initPopuPopu();

        //监听页面输入框变化
        judgeEditTextChange();
    }

    private EasyPopup popu;

    private void initPopuPopu() {
        popu = EasyPopup.create()
                .setContext(activity)
                .setContentView(R.layout.popup_comment_input)
                .setWidth(WindowManager.LayoutParams.FILL_PARENT)
                .setHeight(WindowManager.LayoutParams.FILL_PARENT)
                .setOnViewListener(new EasyPopup.OnViewListener() {
                    @Override
                    public void initViews(View view, EasyPopup basePopup) {

                        initView(view);
                    }

                })
                .setFocusAndOutsideEnable(true)
                .apply();

    }


    private void judgeEditTextChange() {

        etMessage.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (!TextUtils.isEmpty(charSequence) && charSequence.length() > 2) {
                    btnSend.setBackgroundColor(Color.parseColor("#999999"));
                } else {
                    btnSend.setBackgroundColor(Color.parseColor("#007aff"));
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });


    }

    private EditText etMessage;
    private TextView btnSend;
    private View view_cancel;


    private void initView(View view) {

        etMessage = view.findViewById(R.id.reply_et_message);
        btnSend = view.findViewById(R.id.reply_btn_send);
        view_cancel = view.findViewById(R.id.view_cancel);

        btnSend.setOnClickListener(this);
        view_cancel.setOnClickListener(this);
    }


    public void dismiss() {
        popu.dismiss();
    }


    public void showAnswerPopuPopu(View view) {
        initPopuPopu();
        popu.showAtLocation(view, Gravity.TOP, 0, 0);

    }

    public void setInputHead(String userName) {
        etMessage.setText("回复@" + userName+"：");
    }

    public void clearEditTextContent() {
        etMessage.setText("");
    }


    @Override
    public void onClick(View v) {

        int i = v.getId();
        if (i == R.id.reply_btn_send) {

            String comment = etMessage.getText().toString();
            if (!StringUtil.isNotEmpty(comment, true)) {
                ToastManage.s(activity, "请先输入你的看法哦！");
            }
            if (listener != null) {
                listener.onSendMessage(comment);
                popu.dismiss();
            }
        } else if (i == R.id.view_cancel) {
            popu.dismiss();

        }
    }


    public interface BottomCommentListener {
        void onSendMessage(String comment);
    }

    public BottomCommentListener listener;

    public void setBottomCommentListener(BottomCommentListener bottomCommentListener) {

        this.listener = bottomCommentListener;
    }

}
